//
//  SZLoginViewController.m
//  KaiJing
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "SZLoginViewController.h"
#import "SZRegisterViewController.h"
#import "SZForgetPasswordViewController.h"
#import "PKRequestManager.h"
#import "SZLoginMessageModel.h"
#import <MJExtension.h>
#import "SZLoginTool.h"

// 要先从此网址获取参数
#define kLoginBaseURL @"http://morguo.com/member.php?mod=logging&action=login&androidflag=1&appfrom=ios&iosversion=3.13"
// 登陆网址需要拼接最后一个参数
#define kLoginComURL @"http://morguo.com/member.php?mod=logging&androidflag=1&appfrom=ios&iosversion=3.13&action=login&mobile=0&&loginsubmit=yes&loginhash="

@interface SZLoginViewController ()
/** 账号 */
@property (weak, nonatomic) IBOutlet UITextField *userName;
/** 密码 */
@property (weak, nonatomic) IBOutlet UITextField *password;
/** 登陆按钮 */
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
/** 登陆获取的基础信息 */
@property (nonatomic, strong) SZLoginMessageModel *loginBaseModel;
@end

@implementation SZLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackItem];
    _loginButton.layer.cornerRadius = 5;
    _loginButton.layer.masksToBounds = YES;
    _loginButton.hidden = YES;
    if (![[SZLoginTool shareLogin] isLogin]) {
        [self startLogin];// 未登陆进行登陆
    }
}

/**
 *  添加模态回去按钮
 */
- (void)addBackItem {
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    back.frame = CGRectMake(0, 10, 20, 20);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:back];
}
- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 *  开始登陆
 */
- (void)startLogin {
    [PKRequestManager requestWithUrlString:kLoginBaseURL parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        SZLog(@"%@",dataDic);
        _loginBaseModel = [SZLoginMessageModel mj_objectWithKeyValues:dataDic[@"data"]];
        _loginButton.hidden = NO;// 请求到数据,显示登陆按钮
    } error:^(NSError *error) {
        SZLog(@"%@",error);
    }];
}

/**
 *  登录点击
 */
- (IBAction)loginClick {
    NSString *urlString = [NSString stringWithFormat:@"%@%@", kLoginComURL, _loginBaseModel.loginhash];
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    NSUUID *uuid = [UIDevice currentDevice].identifierForVendor;
    parDic[@"deviceid"] = uuid.UUIDString;
    parDic[@"formhash"] = _loginBaseModel.formhash;
    parDic[@"password"] = _password.text;
    parDic[@"referer"] = _loginBaseModel.referer;
    parDic[@"username"] = _userName.text;
    __block SZLoginViewController *tempSelf = self;
    [PKRequestManager requestWithUrlString:urlString parDic:parDic method:POST finish:^(NSDictionary *dataDic) {
        SZLog(@"%@",dataDic);// 登陆成功
        NSString *loginstate = [NSString stringWithFormat:@"%@",dataDic[@"data"][@"success"]];
//        tempSelf.block(dataDic[@"data"][@"username"]);
        if ([loginstate isEqualToString:@"1"]) {
            [[SZLoginTool shareLogin] saveUserMessageWithModel:[SZLoginMessageModel mj_objectWithKeyValues:dataDic[@"data"]]];
            [[SZLoginTool shareLogin] saveLoginStateWithString:@"1"];
            [[SZLoginTool shareLogin] updateFormhash];
        }
        [tempSelf dismissViewControllerAnimated:YES completion:nil];
    } error:^(NSError *error) {
        
        SZLog(@"%@",error);
        
    }];
}

/**
 *  忘记密码
 */
- (IBAction)forgetPassword {
    SZForgetPasswordViewController *forgetPassVC = [[SZForgetPasswordViewController alloc] init];
    forgetPassVC.findPassBaseModel = _loginBaseModel;
    [self.navigationController pushViewController:forgetPassVC animated:YES];
}

/**
 *  注册
 */
- (IBAction)registerClick {
    SZRegisterViewController *registerVC = [[SZRegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}
@end
